<?php
//+---------------------------------------------------------------------------------------------------------------------
//| 人生是荒芜的旅行，冷暖自知，苦乐在心
//+---------------------------------------------------------------------------------------------------------------------
//| Author:Janmas <janmas@126.com>
//+---------------------------------------------------------------------------------------------------------------------
//| 
//+---------------------------------------------------------------------------------------------------------------------


namespace app\common\model;


use app\common\traits\Format;
use app\admin\exception\ModelException;
use think\Exception;
use think\helper\Str;

class BaseModel extends \think\Model
{
	use Format;

	/**
	 * 更新、新增是需要操作的字段
	 * @var string[]
	 */
	protected $submitField = [];

	/**
	 * 根据submitField同步添加（修改）的数据 鸡肋玩意儿 可以不用管直接用tp的修改器
	 * @param $data
	 * @return array
	 */
	public function setField( Array $data )
	{
		if(empty($this->submitField)){
			return $data;
		}
		$newData = [];
		foreach($this->submitField as $key=>$value){
			if(isset($data[$key])){
				if(is_array($value)){
					$format = ucfirst($value[0]);
					$newData[$key] = $this->$format($data[$key],isset($value[1])?$value[1]:false);
				}else{
					$format = ucfirst($value);
					$newData[$key] = $this->$format($data[$key]);
				}
				continue;
			}
			$method = 'setAttr' . Str::studly($key);
			if(!method_exists($this, $method)){
				throw new Exception('方法'.$method.'不存在');
			}
			$newData[$key] = $this->$method($data);
		}
		$newData = array_merge($data,$newData);
		return $newData;
	}

	public function getSubmitField(){
		return array_keys($this->submitField);
	}

	public function getDeleteTime(){
	    if(isset($this->deleteTime)){
            return $this->deleteTime;
        }
    }
}